/*
 * @Description: 配置
 * @Author: chenju
 * @Date: 2022-01-14 14:46:49
 * @LastEditors: qingyang
 * @LastEditTime: 2023-07-25 20:17:43
 */
import { reactive, markRaw } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import { getFactorList, getVersionList } from '@/apis/system/config/factorList'
import type { GetFactorListParams } from '@/apis/system/config/model/factorListModel'
import type { BasicPagingListParams } from '@/apis/basicModel'
import type { SearchOptionsType, CPageTableProps } from '@/components/CPageTable'
import { ColumnSettingTypeEnum } from '@/components/CPageTable'

export const useFactorList = () => {
  // 搜索字段及其他
  const searchForm = reactive<GetFactorListParams>({
    type: null,
    sourceType: null,
    version: null,
    factorName: null
  })

  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '因子名称',
      field: 'factorName',
      type: 'input'
    },
    {
      name: '因子类型',
      field: 'type',
      type: 'select',
      fixedOption: 'factorTypeOptionx'
    },
    {
      name: '因子来源分类',
      field: 'sourceType',
      type: 'select',
      fixedOption: 'FactorSourceTypeOption',
      style: 'width:134px'
    },
    {
      name: '版本号',
      field: 'version',
      type: 'select',
      loadData: (): Promise<any> => {
        return getVersionList().then((res: any) => {
          return handleFormatBankOption(res)
        })
      }
    }
  ])

  function handleFormatBankOption(bankList: any[]) {
    const ans: any[] = []
    for (let i = 0; i < bankList.length; i++) {
      ans.push({
        label: bankList[i],
        value: bankList[i],
        option: bankList[i]
      })
    }
    return ans
  }

  // 表格配置
  const tableOptions: CPageTableProps['tableOptions'] = {
    loadData
  }

  function loadData(parameter: BasicPagingListParams) {
    const params: GetFactorListParams & BasicPagingListParams = Object.assign(
      handleParams(),
      parameter
    )
    return getFactorList(params)
  }

  // 处理参数
  const handleParams = (): GetFactorListParams => {
    // 参数
    let params: GetFactorListParams = {
      type: searchForm.type,
      sourceType: searchForm.sourceType,
      version: searchForm.version,
      factorName: searchForm.factorName
    }
    params = removeUndefinedParams(params)
    return params
  }

  const tableFieldsSetting: CPageTableProps['tableFieldsSetting'] = [
    {
      title: '因子名称/因子编号',
      field: 'factorNo',
      slot: 'factorNoSlot',
      minWidth: 170,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '因子类型/因子来源分类',
      field: 'type',
      slot: 'typeSlot',
      width: 200,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '因子来源/版本号',
      field: 'source',
      slot: 'sourceSlot',
      minWidth: 140,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '活动数据单位',
      field: 'dataUnit',
      width: 120,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '因子单位',
      field: 'unit',
      width: 120,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '启用状态',
      field: 'status',
      slot: 'statusSlot',
      width: 90,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 90,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting
  }
}
